#!/bin/sh

newver=${1%-r[0-9]*}
oldver=${2%-r[0-9]*}

docbookdir=/usr/share/xml/docbook

case "${0##*/}" in
docbook-xsl-ns*)
	suffix=""
	uris="http://cdn.docbook.org/release/xsl
	      http://docbook.sourceforge.net/release/xsl-ns"
;;
docbook-xsl-nons*)
	suffix="-nons"
	uris="http://cdn.docbook.org/release/xsl-nons
	      http://docbook.sourceforge.net/release/xsl"
;;
esac

if [ ! -f /etc/xml/catalog ]; then
	xmlcatalog --noout --create /etc/xml/catalog
fi

if [ -n "$oldver" ]; then # upgrade
	xmlcatalog --noout --del \
		"file://$docbookdir/xsl-stylesheets-$oldver$suffix" \
		etc/xml/catalog
fi

for url in $uris; do
	for rewrite in rewriteSystem rewriteURI; do
		for version in $newver current; do
			xmlcatalog --noout --add "$rewrite" \
				"$url/$version" \
				"file://$docbookdir/xsl-stylesheets-$newver$suffix" \
				etc/xml/catalog
			xmlcatalog --noout --add "$rewrite" \
				"$url/current" \
				"file://$docbookdir/xsl-stylesheets-$newver$suffix" \
				etc/xml/catalog
		done
	done
done
